Skip to content

fix(memberships): skip team-owned memberships in transfer_membership#314

Open
adekbadek wants to merge 2 commits intotrunkfrom
fix/NPPM-2741-team-memberships
Open

fix(memberships): skip team-owned memberships in transfer_membership#314
adekbadek wants to merge 2 commits intotrunkfrom
fix/NPPM-2741-team-memberships

Conversation

@adekbadek
Copy link
Copy Markdown
Member

@adekbadek adekbadek commented Apr 14, 2026

Changes proposed in this Pull Request:

Refs NPPM-2741.

Woocommerce_Membership_Updated::transfer_membership() currently reassigns a user_membership to its new owner by rewriting post_author via wp_update_post. That's fine for standalone memberships, but for team-owned memberships (those with a _team_id meta) ownership doesn't live on post_author – it lives on three separate records:

  • the team post's _member_id (the team owner)
  • the linked subscription's _customer_user
  • team_member user meta (role, added-date)

Rewriting only post_author silently desyncs all of those. On the next renewal dispatch, SkyVerge Teams' owner-vs-customer check can then spawn a duplicate team, and the original team/membership pair gets orphaned.

Today this path is hard to hit because SkyVerge Teams does not fire wc_memberships_user_membership_transferred, so Events::membership_transferred never dispatches the network event for team memberships from the node side. But a future Teams version, a manual dispatch, a mis-linked remote membership, or a reconciliation flow could all reach transfer_membership with a team-owned local membership, and today that would silently corrupt the data. This PR makes the function skip team-owned memberships and log a note instead.

Cheapest guard: get_post_meta( $existing_membership_id, '_team_id', true ) – direct meta read, no object instantiation.

How to test the changes in this Pull Request:

Unit tests – a new test test_transfer_skips_team_owned_membership is added alongside the existing TestMembershipTransfer suite. It sets up a managed user_membership with _team_id=42 meta, invokes transfer_membership() via Reflection, and asserts post_author is unchanged.

docker exec newspack_dev bash -c '/var/scripts/test-php.sh newspack-network --filter TestMembershipTransfer'

Expected: OK (5 tests, 7 assertions).

Other information:

  • New test added (test_transfer_skips_team_owned_membership)
  • Tests run locally
  • PHPCS clean

🤖 Generated with Claude Code

`Woocommerce_Membership_Updated::transfer_membership()` rewrites `post_author`
on the user_membership to reassign ownership. For team-owned memberships
(those with a `_team_id` meta), that's insufficient: true ownership lives on
the team post's `_member_id`, the linked subscription's `_customer_user`, and
team_member user meta. Rewriting only `post_author` silently desyncs those and
can trigger duplicate-team creation on the next renewal dispatch.

Skip the transfer (with a log note) when the membership carries `_team_id`.
Team ownership transfers must be handled at the team level, not via the
user_membership post.

Refs NPPM-2741.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Guards Woocommerce_Membership_Updated::transfer_membership() against corrupting SkyVerge Teams “team-owned” memberships by skipping transfers when a _team_id meta is present, and adds a unit test to cover the new behavior.

Changes:

  • Detects _team_id on an existing managed membership during transfer handling and exits early with a debug log instead of rewriting post_author.
  • Adds a unit test ensuring post_author remains unchanged for team-owned memberships.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
includes/incoming-events/class-woocommerce-membership-updated.php Adds a guard to skip transferring team-owned memberships (identified by _team_id) and logs the skip to avoid ownership desync.
tests/unit-tests/test-membership-transfer.php Adds a unit test verifying team-owned memberships are not transferred via post_author rewrite.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@adekbadek adekbadek marked this pull request as ready for review April 14, 2026 18:49
@adekbadek adekbadek requested a review from a team as a code owner April 14, 2026 18:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants